import Qt 4.7

Rectangle {
    id: container

    width: 525; height: 314
    color: "transparent"
    state: 'show'
    property string mode: "add"
    property string today: daoManager.today();

    signal callUpdateCategories
    signal callUpdateMoneyRegisters
    signal callMessageBox(string mesgBox, string nextView)

    function updateCategories(m_model) {
        dropDown.model = m_model;
    }
    function eraseFields() {
        inButton.turnOff();
        outButton.turnOff();
        inButton.toggle();
        dropDown.idSelected = -1;
        dropDown.textSelected = "Nenhuma";
        descriptionLineEdit.text = "";
        valueLineEdit.text = 0;
        dateInput.text = container.today;
    }
    function activeMoneyEdit() {
        mode = 'edit';
        var money = daoManager.money2BeEdited();
        inButton.turnOff();
        outButton.turnOff();
        if (money.moneyType == 0) {
            inButton.toggle();
        }
        else {
            outButton.toggle();
        }
        var catModel = dropDown.model
        if (money.category) {
            for (var i = 0; i < catModel.length; i++) {
                if (catModel[i].id == money.category.id) {
                    dropDown.textSelected = catModel[i].name
                    dropDown.idSelected = i;
                }
            }
        }
        descriptionLineEdit.text = money.description;
        valueLineEdit.text = money.amount;
        dateInput.text = money.moneyDate;
    }

    MouseArea {
        // The unique purpose of bring back the dropdown
        anchors.fill: parent
        onClicked: {
            if (dropDown.state == 'extended')
                dropDown.state = 'normal';
        }
    }
    DateInput {
        id: dateInput;

        x: 342; y: 22;
    }
    Text {
        id: addFlowLabel

        x: 38; y: 16
        text: "Adicionar"; color: "#00adef"
        font { pointSize: 9; weight: Font.DemiBold; family: "Series 60 Sans" }
    }
    Text {
        id: flowLabel

        x: 60; y: 86
        text: "Fluxo"; color: "black"
        font { pointSize: 7; weight: Font.DemiBold; family: "Series 60 Sans" }
    }
    Button {
        id: inButton

        x: 168; y: 64; width: 136; height: 51
        text: "Entrada"
        toggleEnable: true; toggledColor: "#00adef"; defaultColor: "transparent"
        textColor: "#00adef"
        onClicked: outButton.toggle();
    }
    Button {
        id: outButton

        x: 313; y: 64; width: 136; height: 51;
        text: "Saida";
        toggleEnable: true; toggledColor: "#00adef"; defaultColor: "transparent"
        textColor: "#00adef"

        onClicked: inButton.toggle()
    }
    Text {
        id: categoryLabel

        x: 60; y: 140
        color: "black"; text: "Categoria"
        font { pointSize: 7; weight: Font.DemiBold; family: "Series 60 Sans" }
    }
    Text {
        id: descriptionLabel

        x: 60; y: 200
        text: "Descricao"; color: "black"
        font { pointSize: 7; weight: Font.DemiBold; family: "Series 60 Sans" }
    }
    LineEdit {
        id: descriptionLineEdit

        x: 167; y: 185; width: 282; height: 50
        enabled: dropDown.state == 'normal'
    }
    Text {
        id: valueLabel

        x: 60; y: 252
        text: "Valor"; color: "black"
        font { pointSize: 7; weight: Font.DemiBold; family: "Series 60 Sans" }
    }
    // Needs a regExp to validate the input
    LineEdit {
        id: valueLineEdit

        enabled: dropDown.state == 'normal'
        x: 167; y: 242; width: 138; height: 53
    }
    Button {
        id: okButton

        enabled: dropDown.state == 'normal'
        x: 349; y: 242; width: 101; height: 53
        text: "OK"
        onClicked: {
            var moneyType = 0;
            if (inButton.isToggled) {
                console.debug("QML:AddView - MONEY IN");
                moneyType = 0;
            }
            else if(outButton.isToggled) {
                console.debug("QML:AddView - MONEY OUT");
                moneyType = 1;
            }
            //TODO: Validate the input data
            if (mode == 'add') {
                daoManager.addMoney(moneyType, dropDown.idSelected, descriptionLineEdit.text,
                                valueLineEdit.text, dateInput.text);
                container.callMessageBox("Money Register adicionado!","CashFlowView");
            }
            else {
                daoManager.updateMoneyRegister(moneyType, dropDown.idSelected, descriptionLineEdit.text,
                                     valueLineEdit.text, dateInput.text);
                container.callMessageBox("Money Register modificado!","CashFlowView");
            }
            container.callUpdateMoneyRegisters();
        }
    }
    AdvancedDropDown {
        id: dropDown

        x: 167; y: 128
        width: 190; rowPosX: 130

        onCallUpdateCategories: container.callUpdateCategories();
    }

}
